package com.example.lijuan.myapplication.UI.Activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import com.example.lijuan.myapplication.Adapter.PagerAdapter;
import com.example.lijuan.myapplication.R;
import com.example.lijuan.myapplication.UI.Fragment.FragmentCarInfo;
import com.example.lijuan.myapplication.UI.Fragment.FragmentCarTrip;
import com.example.lijuan.myapplication.UI.Fragment.FragmentTripMain;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

public class MapActivity extends FragmentActivity implements
		TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
	private TabHost mTabHost;
	private ViewPager mViewPager;
	private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabInfo>();
	private PagerAdapter mPagerAdapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// Initialise the TabHost
		this.initialiseTabHost(savedInstanceState);
		if (savedInstanceState != null) {
			mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
			}
		
		// Intialise ViewPager
		this.intialiseViewPager();
	}
	
	private class TabInfo {
		private String tag;
		private Class<?> clss;
		private Bundle args;
		private Fragment fragment;

		TabInfo(String tag, Class<?> clazz, Bundle args) {
			this.tag = tag;
			this.clss = clazz;
			this.args = args;
		}
	}
	 
	// A simple factory that returns dummy views to the Tabhost
	class TabFactory implements TabContentFactory {

		private final Context mContext;

		public TabFactory(Context context) {
			mContext = context;
		}

		public View createTabContent(String tag) {
			View v = new View(mContext);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}}

	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab selected
		super.onSaveInstanceState(outState);
	}

	// Initialise ViewPager
	private void intialiseViewPager() {

		List<Fragment> fragments = new Vector<Fragment>();
		fragments.add(Fragment.instantiate(this, FragmentCarInfo.class.getName()));
		fragments.add(Fragment.instantiate(this, FragmentTripMain.class.getName()));
		
		this.mPagerAdapter = new PagerAdapter(
				super.getSupportFragmentManager(), fragments);
		
		this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
		this.mViewPager.setAdapter(this.mPagerAdapter);
		this.mViewPager.setOnPageChangeListener(this);
	}

	// Initialise the Tab Host
	private void initialiseTabHost(Bundle args) {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
		TabInfo tabInfo = null;
		MapActivity.AddTab(this, this.mTabHost,
				this.mTabHost.newTabSpec("CarInfo").setIndicator("CarInfo"),
				(tabInfo = new TabInfo("CarInfo", FragmentCarInfo.class, args)));
		this.mapTabInfo.put(tabInfo.tag, tabInfo);
		
		MapActivity.AddTab(this, this.mTabHost,
				this.mTabHost.newTabSpec("CarTrip").setIndicator("CarTrip"),
				(tabInfo = new TabInfo("CarTrip", FragmentCarTrip.class, args)));
		this.mapTabInfo.put(tabInfo.tag, tabInfo);
		
		mTabHost.setOnTabChangedListener(this);
	}

	private static void AddTab(MapActivity activity, TabHost tabHost,
			TabHost.TabSpec tabSpec, TabInfo tabInfo) {
		tabSpec.setContent(activity.new TabFactory(activity));
		tabHost.addTab(tabSpec);
	}

	public void onTabChanged(String tag) {
		// TabInfo newTab = this.mapTabInfo.get(tag);
		int pos = this.mTabHost.getCurrentTab();
		this.mViewPager.setCurrentItem(pos);
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {}

	@Override
	public void onPageSelected(int position) {
		this.mTabHost.setCurrentTab(position);
	}

	@Override
	public void onPageScrollStateChanged(int state) {}
}